package c.e.b.h.e.p;

import com.huawei.hms.analytics.core.crypto.AesCipher.AesLen;
import java.io.ByteArrayInputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;

/* compiled from: CodedOutputStream */
public final class c implements Flushable {
    public final byte[] a;
    public final int b;
    public int c = 0;
    public final OutputStream d;

    /* compiled from: CodedOutputStream */
    public static class a extends IOException {
        public a() {
            super("CodedOutputStream was writing to a flat byte array and ran out of space.");
        }
    }

    public c(OutputStream outputStream, byte[] bArr) {
        this.d = outputStream;
        this.a = bArr;
        this.b = bArr.length;
    }

    public static int a(int i, boolean z) {
        return f(i) + 1;
    }

    public static int b(int i, a aVar) {
        return f(i) + (d(aVar.a.length) + aVar.a.length);
    }

    public static int c(int i, int i2) {
        return f(i) + (i2 >= 0 ? d(i2) : 10);
    }

    public static int d(int i) {
        return (i & -128) == 0 ? 1 : (i & -16384) == 0 ? 2 : (-2097152 & i) == 0 ? 3 : (i & -268435456) == 0 ? 4 : 5;
    }

    public static int f(int i) {
        return d((i << 3) | 0);
    }

    public static int i(int i, int i2) {
        return f(i) + d(i2);
    }

    public static int k(int i, long j) {
        i = f(i);
        int i2 = (-128 & j) == 0 ? 1 : (-16384 & j) == 0 ? 2 : (-2097152 & j) == 0 ? 3 : (-268435456 & j) == 0 ? 4 : (-34359738368L & j) == 0 ? 5 : (-4398046511104L & j) == 0 ? 6 : (-562949953421312L & j) == 0 ? 7 : (-72057594037927936L & j) == 0 ? 8 : (j & Long.MIN_VALUE) == 0 ? 9 : 10;
        return i + i2;
    }

    public static c l(OutputStream outputStream) {
        return new c(outputStream, new byte[4096]);
    }

    public void flush() throws IOException {
        if (this.d != null) {
            m();
        }
    }

    public final void m() throws IOException {
        OutputStream outputStream = this.d;
        if (outputStream != null) {
            outputStream.write(this.a, 0, this.c);
            this.c = 0;
            return;
        }
        throw new a();
    }

    public void n(int i, boolean z) throws IOException {
        r((i << 3) | 0);
        q(z);
    }

    public void o(int i, a aVar) throws IOException {
        r((i << 3) | 2);
        r(aVar.a.length);
        byte[] bArr = aVar.a;
        int length = bArr.length;
        int i2 = this.b;
        int i3 = this.c;
        i2 -= i3;
        if (i2 >= length) {
            System.arraycopy(bArr, 0, this.a, i3, length);
            this.c += length;
            return;
        }
        System.arraycopy(bArr, 0, this.a, i3, i2);
        i = i2 + 0;
        length -= i2;
        this.c = this.b;
        m();
        if (length <= this.b) {
            System.arraycopy(aVar.a, i, this.a, 0, length);
            this.c = length;
            return;
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(aVar.a);
        long j = (long) i;
        if (j == byteArrayInputStream.skip(j)) {
            while (length > 0) {
                i = Math.min(length, this.b);
                int read = byteArrayInputStream.read(this.a, 0, i);
                if (read == i) {
                    this.d.write(this.a, 0, read);
                    length -= read;
                } else {
                    throw new IllegalStateException("Read failed.");
                }
            }
            return;
        }
        throw new IllegalStateException("Skip failed.");
    }

    public void p(int i, int i2) throws IOException {
        r((i << 3) | 0);
        if (i2 >= 0) {
            r(i2);
        } else {
            s((long) i2);
        }
    }

    public void q(int i) throws IOException {
        byte b = (byte) i;
        if (this.c == this.b) {
            m();
        }
        byte[] bArr = this.a;
        int i2 = this.c;
        this.c = i2 + 1;
        bArr[i2] = b;
    }

    public void r(int i) throws IOException {
        while ((i & -128) != 0) {
            q((i & 127) | AesLen.ROOTKEY_COMPONET_LEN);
            i >>>= 7;
        }
        q(i);
    }

    public void s(long j) throws IOException {
        while ((-128 & j) != 0) {
            q((((int) j) & 127) | AesLen.ROOTKEY_COMPONET_LEN);
            j >>>= 7;
        }
        q((int) j);
    }

    public void t(int i, int i2) throws IOException {
        r((i << 3) | i2);
    }

    public void u(int i, int i2) throws IOException {
        r((i << 3) | 0);
        r(i2);
    }

    public void v(int i, long j) throws IOException {
        r((i << 3) | 0);
        s(j);
    }
}
